/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package botobjects;

import interfaces.BotObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jibble.pircbot.Colors;

import tools.StringTools;
import database.DBConnectionFactory;

public class Attendance implements BotObject {
   /**
    * Returns information about attendance for the specified event/wave
    * 
    * @param description
    *           The description of the event/wave
    * @return Attendance object. Returns null if no attendance information is found
    */
   public static Attendance getAttendance(final String description) {
	  if (description == null)
		 return null;
	  Attendance out = null;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT mainnick FROM kdprovince");
		 rs = ps.executeQuery();

		 List<String> unregistered = new LinkedList<String>();
		 while (rs.next()) {
			unregistered.add(rs.getString("mainnick"));
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }

		 ps = DBConnectionFactory.getConnectionHandler().getConnection()
			   .prepareStatement("SELECT * FROM attendance WHERE eventdescription = ?");
		 ps.setString(1, description);
		 rs = ps.executeQuery();

		 List<String> attending = new LinkedList<String>();
		 List<String> notattending = new LinkedList<String>();
		 List<String> late = new LinkedList<String>();
		 while (rs.next()) {
			if (unregistered.contains(rs.getString("mainnick"))) {
			   unregistered.remove(rs.getString("mainnick"));
			}
			if ("attending".equals(rs.getString("attendance"))) {
			   attending.add(rs.getString("mainnick"));
			} else if ("not attending".equals(rs.getString("attendance"))) {
			   notattending.add(rs.getString("mainnick"));
			} else {
			   late.add(rs.getString("mainnick") + " (" + rs.getString("attendance") + ")");
			}
		 }

		 out = new Attendance(description, attending, notattending, late, unregistered);
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   private String description = "";
   private List<String> attending = new LinkedList<String>();
   private List<String> notattending = new LinkedList<String>();
   private List<String> late = new LinkedList<String>();

   private List<String> unregistered = new LinkedList<String>();

   public Attendance(final String description, final List<String> attending, final List<String> notattending,
		 final List<String> late, final List<String> unregistered) {
	  this.description = description;
	  this.attending = attending;
	  this.notattending = notattending;
	  this.late = late;
	  this.unregistered = unregistered;
   }

   public List<String> getAttending() {
	  return attending;
   }

   public String getDescription() {
	  return description;
   }

   @Override
   public List<String> getIRCOutput(final int outputType, final String... args) {
	  List<String> out = new LinkedList<String>();
	  if (getDescription() == null || getAttending() == null || getNotattending() == null || getLate() == null
			|| getUnregistered() == null)
		 return out;
	  String output = Colors.DARK_GREEN + "Players attending: ";
	  for (String s : getAttending()) {
		 if (output.length() > 300) {
			out.add(output);
			output = "";
		 }
		 output += Colors.BLUE + s + Colors.NORMAL + ", ";
	  }
	  if (output.length() > 1) {
		 out.add(output);
	  }

	  output = Colors.RED + "Players NOT attending: ";
	  for (String s : getNotattending()) {
		 if (output.length() > 300) {
			out.add(output);
			output = "";
		 }
		 output += Colors.BLUE + s + Colors.NORMAL + ", ";
	  }
	  if (output.length() > 1) {
		 out.add(output);
	  }

	  output = Colors.OLIVE + "Late: ";
	  for (String s : getLate()) {
		 if (output.length() > 300) {
			out.add(output);
			output = "";
		 }
		 output += Colors.BLUE + s + Colors.NORMAL + ", ";
	  }
	  if (output.length() > 1) {
		 out.add(output);
	  }

	  output = Colors.OLIVE + "Unregistered: ";
	  for (String s : getUnregistered()) {
		 if (output.length() > 300) {
			out.add(output);
			output = "";
		 }
		 output += Colors.BLUE + s + Colors.NORMAL + ", ";
	  }
	  if (output.length() > 1) {
		 out.add(output);
	  }
	  return out;
   }

   public List<String> getLate() {
	  return late;
   }

   public List<String> getNotattending() {
	  return notattending;
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... includes) {
	  Map<String, Object> out = new HashMap<String, Object>();
	  if (StringTools.isNullOrEmpty(getDescription()) || getAttending() == null || getNotattending() == null || getLate() == null
			|| getUnregistered() == null)
		 return out;
	  out.put("Description", getDescription());
	  out.put("Attending", getAttending());
	  out.put("Not Attending", getNotattending());
	  out.put("Late", getLate());
	  out.put("Unregistered", getUnregistered());
	  return out;
   }

   public List<String> getUnregistered() {
	  return unregistered;
   }

   @Override
   public boolean saveToDB() {
	  // not used
	  return false;
   }

}
